<?php
session_start();
include("load-settings.php");

if(!isset($_SESSION['user']))
	header("Location: login.php");

$user = mysql_real_escape_string($_SESSION['user']);
$result = mysql_query("SELECT * FROM user WHERE id = $user");
$row = mysql_fetch_array($result);

if($row['type'] != 2)
	header("Location: login.php");
?>
<!doctype html>
<html>
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, maximum-scale=1, initial-scale=1, user-scalable=0">

  <!-- Always force latest IE rendering engine or request Chrome Frame -->
  <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<link rel="icon" href="http://veteranboosters.com/orders/images/favicon.ico" type="image/x-icon"/>
<link rel="shortcut icon" href="http://veteranboosters.com/orders/images/favicon.ico" type="image/x-icon"/>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:400,600,800">

  <!-- Use title if it's in the page YAML frontmatter -->
  <title>Veteran Boosters Member Area</title>


  <!--[if lt IE 9]>
  <script src="../../javascripts/vendor/html5shiv.js" type="text/javascript"></script>
  <script src="../../javascripts/vendor/excanvas.js" type="text/javascript"></script>
  <![endif]-->

  <link href="stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />
  <script src="javascripts/application.js" type="text/javascript"></script>
</head>

<?php
	include("top.php");
?>

  <div class="container-fluid padded">
    <div class="row-fluid">

      <!-- Breadcrumb line -->

      <div id="breadcrumbs">
               <div class="breadcrumb-button blue">
                  <span class="breadcrumb-label"><i class="icon-home"></i> Home</span>
                  <span class="breadcrumb-arrow"><span></span></span>
               </div>

               <div class="breadcrumb-button">
                  <span class="breadcrumb-label">
                     <i class="icon-user"></i> Members
                  </span>
                  <span class="breadcrumb-arrow"><span></span></span>
               </div>
               
               <div class="breadcrumb-button">
                  <span class="breadcrumb-label">
                     <i class="icon-reorder"></i> Overview                  </span>
                  <span class="breadcrumb-arrow"><span></span></span>
               </div>
            </div>
    </div>
  </div>

  <div class="container-fluid padded">
    
  <div class="row-fluid">
   <div class="span12">
      <div class="box">
         <div class="box-header">
            <span class="title">Boosting Orders</span>
         </div>
         <div class="box-content">
            <table class="table table-normal">
               <thead>
                  <tr>
					 <td>Order ID</td>
					 <td>Booster</td>
					 <td>Summoner</td>
					 <td>Region</td>
                     <td>Details</td>
                     <td>Progress</td>
                     <td>Status</td>
                     <td>Actions</td>
                  </tr>
               </thead>
               <tbody>
			   <?php
					
						$result = mysql_query("SELECT * FROM order_record WHERE complete = 1 AND verified_complete = 0") or die(mysql_error());

						if(mysql_num_rows($result) == 0)
						{
							echo "<tr><td colspan='4'>No orders.</td></tr>";
						}
						else
						{
							while($row = mysql_fetch_array($result))
							{
								$subresult = mysql_query("SELECT * FROM booster_order WHERE order_record = ".$row['id']." ORDER BY timestamp DESC LIMIT 1") or die(mysql_error());
								$subrow = mysql_fetch_array($subresult);

								if(mysql_num_rows($subresult) == 0)
								{
									$booster_row['username'] = "No booster";
								}
								else
								{
									$booster_result = mysql_query("SELECT * FROM user WHERE id = ".$subrow['booster']) or die(mysql_error());
									$booster_row = mysql_fetch_array($booster_result);
								}

								if($row['end_tier'] != null && $row['end_tier'] != "")
								{
									echo "<tr>";
									echo "<td>".$row['id']."</td>";
									echo "<td>".$booster_row['username']."</td>";
									echo "<td>".$row['summoner']."</td>";
									echo "<td>".$row['region']."</td>";
									echo "<td>".$row['start_tier']." ".$row['start_division']." - ".$row['end_tier']." ".$row['end_division']."</td>";
									echo "<td>Pending approval</td>";
									echo "<td>Admin reviewing</td>";
									echo "<td><span style = 'margin-right: 10px'><a href='order.php?id=".$row['id']."' class='btn btn-info'>Details</a></span>";
									echo "<span style = 'margin-right: 10px'><a href='approve.php?order=".$row['id']."' class='btn btn-info'>Approve</a></span><span><a href='disapprove.php?order=".$row['id']."' class='btn btn-info'>Disapprove</a></span>";
									echo "</td></tr>";
								}
								else
								{
									$total_result = mysql_query("SELECT SUM(wins) AS all_wins FROM booster_order WHERE order_record = ".$row['id']);
									$total_row = mysql_fetch_array($total_result);
									$total_wins = $total_row['all_wins'];
									$total_result = mysql_query("SELECT SUM(losses) AS all_wins FROM booster_order WHERE order_record = ".$row['id']);
									$total_row = mysql_fetch_array($total_result);
									$total_losses = $total_row['all_wins'];

									echo "<tr>";
									echo "<td>".$row['id']."</td>";
									echo "<td>".$booster_row['username']."</td>";
									echo "<td>".$row['summoner']."</td>";
									echo "<td>".$row['region']."</td>";
									echo "<td>".$row['number_of_wins']." wins at ".$row['start_tier']."</td>";
									echo "<td>".($total_wins-$total_losses)." of ".$row['number_of_wins']."</td>";
									echo "<td>Admin reviewing</td>";
									echo "<td><span style = 'margin-right: 10px'><a href='order.php?id=".$row['id']."' class='btn btn-info'>Details</a></span>";
									echo "<span style = 'margin-right: 10px'><a href='approve.php?order=".$row['id']."' class='btn btn-info'>Approve</a></span><span><a href='disapprove.php?order=".$row['id']."' class='btn btn-info'>Disapprove</a></span>";
									echo "</td></tr>";
									/*$subresult = mysql_query("SELECT * FROM booster_order WHERE order_record = ".$row['id']);
									
									$subrow = mysql_fetch_array($subresult);
									

									$complete = false;

									if(($subrow['wins']-$subrow['losses']) == $row['number_of_wins'])
										$complete = true;

									if($subrow['active'] == 3 && $complete)
									{
										$total_result = mysql_query("SELECT SUM(wins) AS all_wins FROM booster_order WHERE order_record = ".$row['id']);
										$total_row = mysql_fetch_array($total_result);
										$total_wins = $total_row['all_wins'];
										$total_result = mysql_query("SELECT SUM(losses) AS all_wins FROM booster_order WHERE order_record = ".$row['id']);
										$total_row = mysql_fetch_array($total_result);
										$total_losses = $total_row['all_wins'];
										echo "<tr><td>".$row['number_of_wins']." wins at ".$row['start_tier']." ".$row['start_division']."</td>";
										echo "<td>".($total_wins-$total_losses)." of ".$row['number_of_wins']." wins</td>";
										echo "<td>Admin reviewing</td>";
										echo "<td><span style = 'margin-right: 10px'><a href='order.php?id=".$row['id']."' class='btn btn-info'>Details</a></span>";
										echo "<span><a href='approve.php?order=".$row['id']."' class='btn btn-info'>Approve</a></span>";
										echo "</td>";
									}

									echo "</tr>";*/
								}
							}
						}
					
				?>
			   </tbody>
            </table>
         </div>
      </div>
   </div>
</div>
<div class="row-fluid">
   <div class="span12">
      <div class="box">
         <div class="box-header">
            <span class="title">Coaching Orders</span>
         </div>
         <div class="box-content">
            <table class="table table-normal">
               <thead>
                  <tr>
                     <td>Order ID</td>
					 <td>Booster</td>
					 <td>Summoner</td>
					 <td>Region</td>
                     <td>Details</td>
                     <td>Status</td>
                     <td>Actions</td>
                  </tr>
               </thead>
               <tbody>
                  <?php
					
						$result = mysql_query("SELECT * FROM duo_order WHERE complete = 1 AND verified_complete = 0") or die(mysql_error());

						if(mysql_num_rows($result) == 0)
						{
							echo "<tr><td colspan='4'>No orders.</td></tr>";
						}
						else
						{
							while($row = mysql_fetch_array($result))
							{
								$subresult = mysql_query("SELECT * FROM booster_order WHERE order_record = ".$row['id']." ORDER BY timestamp DESC LIMIT 1") or die(mysql_error());
								$subrow = mysql_fetch_array($subresult);

								if(mysql_num_rows($subresult) == 0)
								{
									$booster_row['username'] = "No booster";
								}
								else
								{
									$booster_result = mysql_query("SELECT * FROM user WHERE id = ".$subrow['booster']) or die(mysql_error());
									$booster_row = mysql_fetch_array($booster_result);
								}

								echo "<tr>";
								echo "<td>".$row['id']."</td>";
								echo "<td>".$booster_row['username']."</td>";
								echo "<td>".$row['summoner']."</td>";
								echo "<td>".$row['region']."</td>";
								if($row['number_of_wins'] > 0)
									echo "<td>".$row['number_of_wins']." wins</td>";
								else if($row['number_of_games'] > 0)
									echo "<td>".$row['number_of_games']." games</td>";
								if($row['number_of_coaches'] > 0)
									echo "<td>".$row['number_of_coaches']." coached games</td>";
								echo "<td>Admin reviewing</td>";
								echo "<td><span style = 'margin-right: 10px'><a href='duo_order.php?id=".$row['id']."' class='btn btn-info'>Details</a></span>";
								echo "<span style = 'margin-right: 10px'><a href='approve_duo.php?order=".$row['id']."' class='btn btn-info'>Approve</a></span><span><a href='disapprove_duo.php?order=".$row['id']."' class='btn btn-info'>Disapprove</a></span>";
								echo "</td></tr>";
							}
						}
					
				?>
			   </tbody>
            </table>
         </div>
      </div>
   </div>
</div>
</div>
<?php
	include("bottom.php");
?>